<template>
  <div class="strategy-like-container">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <div class="header-left">
          <h2>
            <i class="icon-star">⭐</i>
            收到的收藏
          </h2>
          <div class="header-subtitle">发现谁收藏了你的精彩攻略</div>
        </div>
        <div class="total-count">
          <span class="count-number">{{ collections.length }}</span>
          <span class="count-text">条收藏</span>
        </div>
      </div>
    </div>

    <!-- 加载状态 -->
    <div v-if="loading" class="loading-container">
      <div class="loading-spinner"></div>
      <span class="loading-text">加载收藏内容中...</span>
    </div>

    <!-- 空状态 -->
    <div v-else-if="collections.length === 0" class="empty-state">
      <div class="empty-icon">
        <i class="icon-empty">💭</i>
      </div>
      <h3 class="empty-title">还没有收到收藏</h3>
      <p class="empty-description">继续创作优质攻略，吸引更多用户收藏吧！</p>
    </div>

    <!-- 收藏列表 -->
    <div v-else class="strategy-list">
      <div v-for="collection in paginatedList" :key="collection.id" class="strategy-card">
        <!-- 收藏者信息区 -->
        <div class="strategy-user">
          <router-link :to="`/user/profile/${collection.userId}`" class="user-link">
            <img :src="collection.userAvatar || defaultAvatar" class="user-avatar" alt="用户头像" />
            <div class="user-info">
              <h3 class="user-name">{{ collection.userName }}</h3>
              <div class="collect-time">{{ collection.collectionDate }}</div>
            </div>
          </router-link>
        </div>
        
        <!-- 攻略信息 -->
        <div class="strategy-content">
          <div class="content-text">
            <router-link :to="`/homepage/strategy/detail/${collection.strategyId}`" class="strategy-link">
              <h3 class="strategy-title">{{ collection.strategyName }}</h3>
            </router-link>
            <div class="strategy-meta">
              <div class="meta-item">
                <i class="meta-icon">⏱️</i>
                <span>收藏时间：{{ collection.collectionDate }}</span>
              </div>
            </div>
          </div>
        </div>
        
        <!-- 攻略图片 -->
        <router-link :to="`/homepage/strategy/detail/${collection.strategyId}`" class="strategy-img-container">
          <img :src="collection.strategyImageLink" class="strategy-img" alt="攻略图片" />
          <div class="collect-badge">
            <i class="badge-icon">⭐</i>
          </div>
        </router-link>
      </div>
    </div>

    <!-- 分页器 -->
    <div v-if="collections.length > 0" class="pagination-container">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="collections.length"
        :page-size="pageSize"
        :current-page="currentPage"
        @current-change="handlePageChange"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { ElMessage } from 'element-plus';
import axios from 'axios';

export default {
  name: 'GetCollecInfo',
  data() {
    return {
      collections: [],
      loading: true,
      pageSize: 8,
      currentPage: 1,
      defaultImg: "https://img1.baidu.com/it/u=2331851122,2068186006&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=333",
      baseAvatarUrl: 'http://localhost:8080/uploads/avatar/',
      defaultAvatar: 'https://cdn.jsdelivr.net/gh/baimingxuan/media-store/avatar-default.png',
      baseScenicSpotImageUrl: 'http://localhost:8080/uploads/scenicspot/'
    };
  },
  computed: {
    paginatedList() {
      const start = (this.currentPage - 1) * this.pageSize;
      return this.collections.slice(start, start + this.pageSize);
    }
  },
  methods: {
    handlePageChange(page) {
      this.currentPage = page;
    },
    goToStrategyDetail(strategyId) {
      this.$router.push(`/homepage/strategy/detail/${strategyId}`);
    },
    async fetchCollections() {
      this.loading = true;
      try {
        const userAccount = localStorage.getItem('userAccount');
        if (!userAccount) {
          ElMessage.warning('请先登录');
          this.loading = false;
          return;
        }
        
        const response = await axios.get(`http://localhost:8080/strategyCollect/myFavorites/${encodeURIComponent(userAccount)}`);
        
        this.collections = response.data.map(collection => ({
          id: collection.id,
          userId: collection.userId,
          userName: collection.userName,
          userAvatar: collection.userAvatar ? 
            (collection.userAvatar.startsWith('http') ? 
              collection.userAvatar : 
              this.baseAvatarUrl + collection.userAvatar) : 
            this.defaultAvatar,
          strategyId: collection.strategyId,
          strategyName: collection.strategyName,
          strategyImageLink: collection.strategyImageLink ? 
            (collection.strategyImageLink.startsWith('http') ? 
              collection.strategyImageLink : 
              this.baseScenicSpotImageUrl + collection.strategyImageLink) : 
            this.defaultImg,
          favoriteTime: collection.favoriteTime,
          collectionDate: new Date(collection.favoriteTime).toLocaleString('zh-CN', {
            year: 'numeric',
            month: '2-digit',
            day: '2-digit',
            hour: '2-digit',
            minute: '2-digit'
          }),
        }));
      } catch (error) {
        console.error('获取收藏失败:', error);
        ElMessage.error('获取收藏失败，请稍后再试');
      } finally {
        this.loading = false;
      }
    },
  },
  mounted() {
    this.fetchCollections();
  },
};
</script>

<style scoped>
.strategy-like-container {
  padding: 24px;
  max-width: 1200px;
  margin: 0 auto;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  min-height: 100vh;
}

/* 页面头部样式 */
.page-header {
  margin-bottom: 32px;
}

.header-content {
  background: white;
  padding: 24px 32px;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left h2 {
  font-size: 28px;
  font-weight: 700;
  color: #2c3e50;
  margin: 0 0 8px 0;
  display: flex;
  align-items: center;
  gap: 12px;
}

.icon-star {
  font-size: 24px;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.1); }
}

.header-subtitle {
  font-size: 14px;
  color: #7f8c8d;
  margin: 0;
}

.total-count {
  display: flex;
  flex-direction: column;
  align-items: center;
  background: linear-gradient(135deg, #a6a6e4 0%, #c3aadd 100%);
  color: white;
  padding: 16px 20px;
  border-radius: 12px;
  min-width: 80px;
}

.count-number {
  font-size: 24px;
  font-weight: 700;
  line-height: 1;
}

.count-text {
  font-size: 12px;
  opacity: 0.9;
  margin-top: 4px;
}

/* 加载状态 */
.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 300px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.loading-spinner {
  width: 48px;
  height: 48px;
  border: 4px solid #f3f4f6;
  border-top: 4px solid #667eea;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.loading-text {
  font-size: 16px;
  color: #6b7280;
  font-weight: 500;
}

/* 空状态 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: white;
  border-radius: 16px;
  padding: 60px 40px;
  text-align: center;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.empty-icon {
  font-size: 64px;
  margin-bottom: 24px;
  opacity: 0.6;
}

.empty-title {
  font-size: 20px;
  font-weight: 600;
  color: #374151;
  margin: 0 0 12px 0;
}

.empty-description {
  font-size: 16px;
  color: #6b7280;
  margin: 0 0 32px 0;
  line-height: 1.5;
}

/* 收藏列表 - 修改为长条形布局 */
.strategy-list {
  display: grid;
  grid-template-columns: 1fr;
  gap: 20px;
}

.strategy-card {
  background: white;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  position: relative;
  padding: 24px;
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 20px;
  align-items: flex-start;
}

.strategy-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
}

/* 左侧信息区域 */
.strategy-user {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 120px;
}

.user-link {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-decoration: none;
  color: inherit;
}

.user-avatar {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  object-fit: cover;
  border: 3px solid #e2e8f0;
  transition: all 0.3s ease;
  margin-bottom: 12px;
}

.user-link:hover .user-avatar {
  border-color: #667eea;
  transform: scale(1.05);
}

.user-info {
  text-align: center;
}

.user-name {
  font-size: 16px;
  font-weight: 600;
  color: #2c3e50;
  margin: 0 0 4px 0;
}

.collect-time {
  font-size: 12px;
  color: #9ca3af;
}

/* 中间内容区域 */
.strategy-content {
  flex: 1;
  padding: 0 16px;
}

.content-text {
  background: #f8fafc;
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 16px;
  border: 1px solid #e2e8f0;
}

.strategy-link {
  text-decoration: none;
  color: inherit;
}

.strategy-title {
  font-size: 18px;
  font-weight: 600;
  color: #2c3e50;
  margin: 0 0 12px 0;
  transition: color 0.2s ease;
}

.strategy-link:hover .strategy-title {
  color: #667eea;
}

.strategy-meta {
  display: flex;
  gap: 16px;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: #9ca3af;
}

.meta-icon {
  font-size: 14px;
}

/* 右侧图片区域 */
.strategy-img-container {
  position: relative;
  width: 240px;
  height: 180px;
  overflow: hidden;
  cursor: pointer;
}

.strategy-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.5s ease;
}

.strategy-card:hover .strategy-img {
  transform: scale(1.05);
}

.collect-badge {
  position: absolute;
  top: 12px;
  right: 12px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  padding: 6px 12px;
  display: flex;
  align-items: center;
  font-size: 14px;
  color: #f1c40f;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.badge-icon {
  font-size: 12px;
  animation: heartbeat 1.5s ease-in-out infinite;
}

@keyframes heartbeat {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.2); }
}

/* 分页器 */
.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 40px;
  padding: 20px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .strategy-like-container {
    padding: 16px;
  }
  
  .header-content {
    flex-direction: column;
    gap: 16px;
    text-align: center;
  }
  
  .strategy-card {
    grid-template-columns: 1fr;
    gap: 16px;
    padding: 20px;
  }
  
  .strategy-user {
    flex-direction: row;
    align-items: center;
    gap: 16px;
  }
  
  .user-link {
    flex-direction: row;
    text-align: left;
  }
  
  .user-avatar {
    margin-bottom: 0;
    width: 48px;
    height: 48px;
  }
  
  .user-info {
    text-align: left;
  }
}

@media (max-width: 480px) {
  .header-content {
    padding: 20px;
  }
  
  .header-left h2 {
    font-size: 24px;
  }
  
  .strategy-content {
    padding: 0;
  }
  
  .strategy-img-container {
    width: 100%;
    height: 200px;
  }
}
</style>